package com.ruoyi.controller;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.domain.vo.BehaviorPunchingRecordVo;
import com.ruoyi.domain.vo.BehaviorsCo2ReductionVo;
import com.ruoyi.service.ILowCarbonBehaviorService;
import com.ruoyi.system.service.IBehaviorService;
import com.ruoyi.system.domain.BehaviorRecode;
import com.ruoyi.system.service.IBehaviorRecodeService;
import com.ruoyi.system.service.impl.BehaviorServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/wechatApp/lowCarbonBehavior")
public class LowCarbonBehaviorController extends BaseController {

    @Autowired
    private ILowCarbonBehaviorService lowCarbonBehaviorService;

    @Autowired
    private IBehaviorService behaviorService;

    @Autowired
    private IBehaviorRecodeService behaviorRecodeService;
    /**
     *  @return 低碳行为列表
     */
    @GetMapping("/getLowCarbonBehaviorList")
    public AjaxResult getLowCarbonBehaviorList() {
        return AjaxResult.success(lowCarbonBehaviorService.getLowCarbonBehaviorList());
    }

    /**
     *  获取单日各类别的低碳行为总减碳量
     * @param userId 用户id
     * @return 各类别的低碳行为总减碳量
     */
    @GetMapping("/getBehaviorsCo2Reduction")
    public AjaxResult getBehaviorsCo2Reduction(@RequestParam("userId") Long userId) {
//        System.out.println("userId:" + userId);
        List<BehaviorsCo2ReductionVo> list = lowCarbonBehaviorService.getBehaviorsCo2Reduction(userId);
//        System.out.println("list:" + list);
        AjaxResult ajaxResult = AjaxResult.success(list);
//        System.out.println("ajaxResult:" + ajaxResult);
        return ajaxResult;
    }

    /**
     * 低碳行为打卡
     * @param
     */
    @PostMapping("/behaviorPunchCard")
    public AjaxResult behaviorPunchCard(@RequestBody BehaviorPunchingRecordVo behaviorPunchingRecordVo) {
        LocalDate today = LocalDate.now();
        Double reduceCo2Today = lowCarbonBehaviorService.getReduceCo2Today(behaviorPunchingRecordVo.getUserId(),behaviorPunchingRecordVo.getBehaviorCode(),today);
        System.out.println("reduceCo2Today:" + reduceCo2Today);
        Double limitation = lowCarbonBehaviorService.getReduceCo2LimitationByBehaviorCode(behaviorPunchingRecordVo.getBehaviorCode());
        System.out.println("limitation:" + limitation);
        Double reduceCo2 = lowCarbonBehaviorService.getReduceCo2ByBehaviorValue(behaviorPunchingRecordVo.getBehaviorValue(), behaviorPunchingRecordVo.getBehaviorCode());
        System.out.println("reduceCo2:" + reduceCo2);
        if (reduceCo2Today >= limitation) {
            return AjaxResult.error("今日该低碳行为的减碳量已达到上限!");
        }
        else if (reduceCo2Today + reduceCo2 > limitation) {
            lowCarbonBehaviorService.behaviorPunchCardSub(behaviorPunchingRecordVo, limitation - reduceCo2Today);
            return AjaxResult.success("今日该低碳行为的减碳量已达到上限!");
        }
        lowCarbonBehaviorService.behaviorPunchCard(behaviorPunchingRecordVo);
        return AjaxResult.success();
    }

    /**
     *  低碳行为打卡
     * @param behaviorRecodeVo 打卡信息
     * @return 打卡结果
     */
//    @PostMapping("/BehaviorPunchCard")
//    public AjaxResult BehaviorPunchCard(@RequestBody BehaviorRecodeVo behaviorRecodeVo) {
//        LocalDate today = LocalDate.now();
//        Long behaviorCode = lowCarbonBehaviorService.getBehaviorCodeByName(behaviorRecodeVo.getBehaviorName());
//        Long reduceCo2Today = lowCarbonBehaviorService.getReduceCo2Today(behaviorRecodeVo.getUserId(),behaviorCode,today);
//
//        if(reduceCo2Today == null){reduceCo2Today = 0L;}
//
//        System.out.println("reduceCo2Today:" + reduceCo2Today);
//        Long limitation = lowCarbonBehaviorService.getReduceCo2LimitationByName(behaviorRecodeVo.getBehaviorName());
//
//        if(behaviorRecodeVo.getReduceCo2()+reduceCo2Today > limitation){
//            return AjaxResult.error("超过该行为减碳量的限额!");
//        }else {
//            BehaviorRecode behaviorRecode = new BehaviorRecode();
//            behaviorRecode.setBehaviorType(lowCarbonBehaviorService.getBehaviorTypeIdByType(behaviorRecodeVo.getBehaviorType()));
//            behaviorRecode.setBehaviorCode(behaviorCode);
//            behaviorRecode.setReduceCo2(behaviorRecodeVo.getReduceCo2());
//            behaviorRecode.setUserId(behaviorRecodeVo.getUserId());
//            behaviorRecode.setRecordDate(new Date());
//            behaviorRecode.setEarnPoints(behaviorRecodeVo.getEarnPoints());
//            return AjaxResult.success(lowCarbonBehaviorService.BehaviorPunchCard(behaviorRecode));
//        }
//    }
    /**
     * 获取低碳行为的折算系数
     * @param behaviorCode 低碳行为编码
     * @return 折算系数
     */
    @GetMapping("/getConversionRateByBehaviorCode")
    public AjaxResult getConversionRateByBehaviorCode(@RequestParam("behaviorCode") Long behaviorCode){
        return AjaxResult.success(lowCarbonBehaviorService.getConversionRateByBehaviorCode(behaviorCode));
    }
}
